<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="30vw" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false"
	           :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
			<el-form-item label="项目名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入项目名称" clearable autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="启用时间" prop="start_time">
				<el-date-picker type="datetime" format="YYYY-MM-DD HH:mm:ss"
				                date-format="MMM DD, YYYY"
				                time-format="HH:mm" placeholder="请选择启用时间" v-model="form.start_time" style="width: 100%;"></el-date-picker>
			</el-form-item>
			<el-form-item label="到期时间" prop="expire_time">
				<el-date-picker type="datetime" format="YYYY-MM-DD HH:mm:ss"
				                date-format="MMM DD, YYYY"
				                time-format="HH:mm" placeholder="请选择到期时间" v-model="form.expire_time" style="width: 100%;"></el-date-picker>
			</el-form-item>
			<el-form-item label="负责人" prop="director">
				<el-select v-model="form.director" filterable clearable placeholder="请选择" style="width:100%">
					<el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"/>
				</el-select>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="是否禁用" prop="system_disable">
				<el-switch v-model="form.system_disable" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	components: {},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增项目',
				edit: '编辑项目',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				name:'',
				system_disable:0,
				start_time:'',
				expire_time:'',
				remark:'',
				director:'',
			},
			//验证规则
			rules: {
				name: [
					{ required: true, message: '请输入项目名称' }
				],
				director: [
					{ required: true, message: '请选择负责人' }
				],
				system_disable: [
					{ required: true, message: '请选择是否禁用', trigger: 'change' }
				],
			},
			adminList: [],
		}
	},
	mounted() {
		this.getUser()
	},
	methods: {
		/**获取员工*/
		async getUser() {
			var res = await this.$API.system.admin.all.get();
			this.adminList = res.data;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.form.start_time = this.common.formatDateTime(this.form.start_time)
			this.form.expire_time = this.common.formatDateTime(this.form.expire_time)
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.basic_new.projeck.edit.post(this.form);
					} else {
						res = await this.$API.basic_new.projeck.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			// Object.assign(this.form, data)
			var reqData = {id: data.id}
			var res = await this.$API.basic_new.projeck.detail.get(reqData);
			Object.assign(this.form, res.data)
		},
	}
}
</script>

<style lang="scss">
.el-form {
	max-height: 700px;
	overflow: hidden;
	overflow-y: auto;

}
</style>
